<template>
  <div class='container'>
    <div v-if="type==='view'">
      <div class="title">
         <h2>{{form.title}}</h2>
         <div>
          <span>{{formatDate(form.createTime)}}</span>
          <span>{{formatUserID(form.creatorID)}}</span>
          <span><i class="el-icon-view"></i></span>
          <span>{{form.visits}}</span>
         </div>
      </div>

     <div class="content" v-html="form.articleBody">

     </div>
    </div>
      <el-form ref="formName"  :rules="rules" :model="form" label-width="80px" v-else>
        <el-form-item label="标题" prop="title">
          <el-input placeholder="请输入文章标签" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="articleBody">
          <quill-editor :options="editorOption" v-model="form.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL">
            <el-input placeholder="请输入视频地址" v-model="form.videoURL"></el-input>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="end">
            <el-button @click="hCancel" >取消</el-button>
            <el-button type="primary" @click="submit">确认</el-button>
          </el-row>
        </el-form-item>
      </el-form>

  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import moment from 'moment'
import { quillEditor } from 'vue-quill-editor'
import * as users from '@/api/base/users.js'
import { detail, add, update } from '@/api/hmmm/articles'

export default {
  components: {
    quillEditor
  },
  props: {
    curId: {
      default: ''
    },
    type: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // toggled buttons
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote', 'code-block'],
            ['code-block', 'image', 'link']
          ]
        },
        placeholder: ''
      },
      userEnu: {},
      form: {
        articleBody: '',
        createTime: '',
        creatorID: '',
        state: '',
        title: '',
        videoURL: '',
        visits: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标签', trigger: blur }

        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: blur }
          // { min: 1, max: 10000, message: '请输入1-10000字的文章标签', trigger: blur }
        ]

      }
    }
  },
  created () {
    if (this.type !== 'add') {
      this.doView()
    }
  },
  methods: {
    hCancel () {
      this.$emit('close')
    },
    async doAdd () {
      try {
        const res = await add(this.form)
        console.log('add', res)
        if (res.status !== 200) return this.$message.error('数据获取失败')
        this.$message.success('添加成功')
        this.$emit('success')
        // 保存数据
      } catch (err) {
        console.log('add', err)
      }
    },
    async doEdit () {
      try {
        const res = await update(this.form)
        console.log('update', res)
        if (res.status !== 200) return this.$message.error('编辑失败')
        this.$message.success('修改成功')
        this.$emit('success')
        // 保存数据
      } catch (err) {
        console.log('update', err)
      }
    },
    async  doView () {
      const view = await detail({ id: this.curId })
      const userList = await users.list()
      console.log(userList, 'userList')
      userList.data.list.forEach(item => {
        this.userEnu[item.id] = item.username
      })
      this.form = view.data
    },
    formatUserID (id) {
      return this.userEnu[id]
    },
    submit () {
      this.$refs.formName.validate((valid) => {
        if (!valid) return
        // console.log('检验成功')
        if (this.type === 'add') {
          this.doAdd()
        } else {
          this.doEdit()
        }
      })
    },
    formatDate (date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang='less'>
  .title{
    border-bottom: 1px dashed #ccc;
    padding-bottom: 10px;
    h2{
      margin:0 ;
      padding: 0;
      font-size: 21px;
    }
    div {
      height: 25px;
   span{
    margin-left: 10px;
    margin-top: 10px;
    line-height: 25px;
    }
    }

  }
  .content{
    padding: 10px;
    background-color: #f5f5f5;
  }
  .quill-editor {
   ::v-deep .ql-editor {
  height: 200px;
  }}
</style>
